define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index',
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'product',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('标题')},
                        {field: 'price', title: __('价格')},
                        {field: 'type', title: __('类型'),formatter: Table.api.formatter.status, searchList: {'1': '个人模板', '2': '企业公司', '3': '实体店铺', '4': '电商行业'}},
                        {field: 'preview_img', title: __('名片图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                                {
                                    name: '生成案例内容',
                                    text: __('生成案例内容'),
                                    extend:'data-area=["70%","90%"]',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: function (row) {
                                        return 'product/add_case?id='+row.id+'&type='+row.type;
                                    },
                                }, {
                                    name: '智能生成名片',
                                    text: __('智能生成名片'),
                                    extend:'data-area=["70%","90%"]',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: function (row) {
                                        return 'product/add_case_s?id='+row.id+'&type='+row.type;
                                    },
                                }
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $('.commonsearch-table').removeClass('hidden');

        },
        add_case: function () {
            Controller.api.bindevent();
            $(document).ready(function () {
                $('.select2').select2({
                    placeholder: '请选择标签', // 选择框的占位文本
                });
            });
          
        },
        add_case_s: function () {
            Controller.api.bindevent();

            $(document).on("click", "#tj", function () {
                var str = '<div><div class="form-group">'+
                '<label for="c-nickname" class="control-label col-xs-12 col-sm-2">标题:</label>'+
                '<div class="col-xs-12 col-sm-4">'+
                '<input data-rule="required"    class="form-control" name="row[json][title][]" type="text" >'+
                '</div>'+
                '<label for="c-nickname" class="control-label col-xs-12 col-sm-1">内容:</label>'+
                '<div class="col-xs-12 col-sm-4">'+
                '<input data-rule="required"    class="form-control" name="row[json][content][]" type="text" >'+
                '</div></div>'+
                '<div class="form-group"  style="border-bottom: 1px solid #eee;padding-bottom: 15px;"><label for="c-nickname" class="control-label col-xs-12 col-sm-2">x轴y轴:</label>'+
                '<div class="col-xs-12 col-sm-2">'+
                '<input data-rule="required"    class="form-control" name="row[json][zhou][]" type="text" >'+
                '</div>'+
                '<label for="c-nickname" class="control-label col-xs-12 col-sm-1">字体大小:</label>'+
                '<div class="col-xs-12 col-sm-2">'+
                '<input data-rule="required"    class="form-control" name="row[json][size][]" type="text" >'+
                '</div>'+
                '<label for="c-nickname" class="control-label col-xs-12 col-sm-1">颜色:</label>'+
                '<div class="col-xs-12 col-sm-2">'+
                '<input data-rule="required"    class="form-control" name="row[json][color][]" type="text" >'+
                '</div>'+
                '</div></div>';
                $("#neirong").append(str);
            });
            $(document).on("click", "#yl", function () {
                let json = $('#edit-form').serialize();
                $.ajax({
                    url: "product/get_yl_img",
                    type: 'post',
                    dataType: 'json',
                    data: json,
                    success: function (res) {
                        if(res.code == 1){
                            $('#c-avatars').attr('src',res.url);
                            $('#preview_img').val(res.url);
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                });
            });
            
            
        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
